/********************************************************************

 modinit.c - standard initialisation for Opus 5 modules
             ( ... slightly changed by me ... :-) )
 
 ********************************************************************/

#ifndef _DOPUS_MODULE_DEF
#define _DOPUS_MODULE_DEF
#include <dopus/modules.h>
#endif

#ifndef CLIB_EXEC_PROTOS_H
#include <clib/exec_protos.h>
#include <pragmas/exec_pragmas.h>
#endif

#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif

#ifndef CLIB_LOCALE_PROTOS_H
#include <clib/locale_protos.h>
#include <pragmas/locale_pragmas.h>
#endif

/********************************************************************/

// some prototypes for the functions here
// needed by SAS to create the library header
int  __saveds __UserLibInit( void );
void __saveds __UserLibCleanup( void );

struct Library *DOSBase;
struct Library *AslBase;
struct Library *GfxBase;
struct Library *DOpusBase;
struct Library *LayersBase;
struct Library *LocaleBase;
struct Library *UtilityBase;
struct Library *DiskfontBase;
struct Library *GadToolsBase;
struct Library *IntuitionBase;
struct Library *CxBase;

#ifdef DATATYPES
struct Library *DataTypesBase;
#endif
#ifdef AREXX
struct Library *RexxSysBase; 
#endif

// our module memorypool
APTR mempool;

// NEW !! our globally IPC pointer
IPCData *exchange;

// Locale pointer
struct DOpusLocale *locale;

// a prototype from buildinstrings.c
extern void init_locale_data(struct DOpusLocale *locale);

/********************************************************************/

// Library initialisation code
int __saveds __UserLibInit()
{
        // Initialise pointers
        AslBase = 0;
        GfxBase = 0;
        DOpusBase = 0;
        LayersBase = 0;
        LocaleBase = 0;
        UtilityBase = 0;
        DiskfontBase = 0;
        GadToolsBase = 0;
        IntuitionBase = 0;
        CxBase = 0;
        locale = 0;
        mempool = NULL;
        exchange = NULL;
                  
#ifdef DATATYPES                  
        DataTypesBase = 0;
#endif            
#ifdef AREXX
        RexxSysBase = 0;
#endif
                                 
        // Get DOS library (can't really fail)
        DOSBase = OpenLibrary( "dos.library", 0 );

        // Open other libraries we need
        if( !(AslBase = OpenLibrary("asl.library", 37))              ||
            !(CxBase  = OpenLibrary("commodities.library", 37))      ||
            !(GfxBase = OpenLibrary("graphics.library", 37))         ||          
            !(DOpusBase = OpenLibrary("dopus5.library", 55))         ||
            !(LayersBase = OpenLibrary("layers.library", 37))        ||
            !(UtilityBase = OpenLibrary("utility.library", 37))      ||            
            !(GadToolsBase = OpenLibrary( "gadtools.library", 37))   ||
            !(IntuitionBase = OpenLibrary( "intuition.library", 37)) ||
                                
#ifdef DATATYPES                                
            !(DataTypesBase = OpenLibrary( "datatypes.library", 39)) ||
#endif                          
#ifdef AREXX
            !(RexxSysBase = OpenLibrary("rexxsyslib.library", 0))    ||
#endif      
            !(DiskfontBase = OpenLibrary("diskfont.library", 37)) )
             return 1;
        
        // Creating our memorypool and use it immediate for the locale
        if( !(mempool = NewMemHandle(4096, 3072, MEMF_CLEAR|MEMF_PUBLIC)) ||
            !(locale = AllocMemH(mempool, sizeof(struct DOpusLocale))) )
             return 1;

        init_locale_data(locale);

        // Open locale library
        if( LocaleBase = OpenLibrary("locale.library", 38) )
          {
             // Store library pointer
             locale->li_LocaleBase = LocaleBase;

             // Open catalog if name supplied
             if( module_info.locale_name )
               {
                  struct TagItem tags[2];

                  // If MODULEF_CATALOG_VERSION is set, we do version checking
                  tags[0].ti_Tag = (module_info.flags & MODULEF_CATALOG_VERSION) ? OC_Version : TAG_IGNORE;
                  tags[0].ti_Data = module_info.ver;
                  tags[1].ti_Tag = TAG_DONE;

                  // Open catalog
                  locale->li_Catalog = OpenCatalogA( NULL, module_info.locale_name, tags );
               }

             // Get default lolale
             locale->li_Locale = OpenLocale( 0 );
          }
        
        return NULL; // Succeeded
}


// Clean up
void __saveds __UserLibCleanup()
{
     if( mempool )
       {
          if( locale )
            {
               if( LocaleBase )
                 {
                    CloseLocale( locale->li_Locale );
                    CloseCatalog( locale->li_Catalog );
                    CloseLibrary( LocaleBase );
                 }

               FreeMemH(locale);
            }

          FreeMemHandle( mempool );
       }
                  
     // Close libraries
     CloseLibrary( AslBase );
     CloseLibrary( CxBase );
     CloseLibrary( GfxBase );
     CloseLibrary( DOpusBase );
     CloseLibrary( LayersBase );
     CloseLibrary( DiskfontBase );
     CloseLibrary( UtilityBase );
                  
#ifdef DATATYPES
     CloseLibrary( DataTypesBase );
#endif            
#ifdef AREXX              
     CloseLibrary( RexxSysBase );
#endif        

     CloseLibrary( GadToolsBase );
     CloseLibrary( IntuitionBase );
     CloseLibrary( DOSBase );
}

/********************************************************************/

// This routine is called by DOpus to find out what the module does
// Do not modify it or move it to an other place !!

ModuleInfo *__asm __saveds L_Module_Identify( register __d0 int num )
{
     // Return module information
     if( num == -1 )
          return &module_info;

     // Valid function number?
     if( num > module_info.function_count ||
         !(module_info.function[num].desc) )
          return 0;

     // Return function description
     return (ModuleInfo *) DOpusGetString( locale, module_info.function[num].desc );
}

/********************************************************************/